package cn.darkhorse.mesadmin.controller.ticket;


import cn.darkhorse.ticket.basis.pojo.TicketRouting;
import cn.darkhorse.ticket.basis.service.TicketRoutingService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;

import cn.darkhorse.ticket.basis.vo.TicketRoutingVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 工艺路线 前端控制器
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/mesticket/basisRouting")
public class TicketRoutingController extends BaseController{
	
	@Resource
	private TicketRoutingService routingService;
	@Resource
	private RepeatCheck<TicketRoutingVO> repeatCheck;
	
	/**
	 * 列表
	 * @param code
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public TableDataInfo list(String code, String name){
		startPage();
		QueryWrapper<TicketRouting> qw = new QueryWrapper<>();
		LambdaQueryWrapper<TicketRouting> queryWrapper = qw.select("DISTINCT CODE,NAME ").lambda();
		if(StringUtils.isNotBlank(code)){
			queryWrapper.like(TicketRouting::getCode,code);
		}
		if(StringUtils.isNotBlank(name)){
			queryWrapper.like(TicketRouting::getName,name);
		}
		List<TicketRouting> routingList = routingService.list(queryWrapper);
		Collections.reverse(routingList);
		return getDataTable(routingList);
	}
	
	/**
	 * 新增
	 * @param routingVO
	 * @return
	 */
	@RepeatSubmit
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public AjaxResult add(@RequestBody TicketRoutingVO routingVO){
		List<TicketRouting> routingList = new ArrayList<>();
		//校验编码、名称
		//String s = routingService.repeat(routingVO);
		String s = repeatCheck.repeat(routingService, routingVO, TicketConstants.BUSINESS_TYPE_GYLX);
		if(StringUtils.isNotBlank(s)){
			routingVO.setCode(s);
		}
		List<TicketRoutingVO.Details> details = routingVO.getDetails();
		if(null != details && details.size()>0){
			details.forEach(item->{
				TicketRouting routing = new TicketRouting();
				routing.setCode(routingVO.getCode());routing.setName(routingVO.getName());routing.setRemarks(routingVO.getRemarks());routing.setIsAutomatically(routingVO.getIsAutomatically());
				routing.setProcedureId(item.getId());routing.setProcedureCode(item.getCode());routing.setProcedureName(item.getName());
				routing.setProcedureStates(item.getInspectionStates());routing.setOrderNumber(item.getOrderNumber());
			  routingList.add(routing);
			});
		}
		
		routingService.saveBatch(routingList);
		return AjaxResult.success();
	}
	
	/**
	 * 修改
	 * @param routingVO
	 * @return
	 */
	@RepeatSubmit
	@RequestMapping(value = "/modify",method = RequestMethod.POST)
	public AjaxResult modify(@RequestBody TicketRoutingVO routingVO){
		boolean quote = routingService.isQuote(routingVO.getCode());
		if(!quote){
			return AjaxResult.error("该工艺路线已被产品引用,无法操作！");
		}
		//删除
		delete(routingVO.getCode());
		//校验名称
		//routingService.repeat(routingVO);
		repeatCheck.repeat(routingService, routingVO, TicketConstants.BUSINESS_TYPE_GYLX);
	
		//添加
		add(routingVO);
		return AjaxResult.success();
	}
	
	/**
	 * 删除
	 * @param code
	 * @return
	 */
	@RepeatSubmit(interval = 2000)
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public AjaxResult delete(String code){
		boolean quote = routingService.isQuote(code);
		if(!quote){
			return AjaxResult.error("该工艺路线已被产品引用,无法操作！");
		}
		routingService.remove(new LambdaQueryWrapper<TicketRouting>().eq(TicketRouting::getCode,code));
		return AjaxResult.success();
	}
	
	
	/**
	 * 查看
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/view",method = RequestMethod.GET)
	public AjaxResult view(String code){
		TicketRoutingVO vo = new TicketRoutingVO();
		List<TicketRoutingVO.Details> details = new ArrayList<>();
		List<TicketRouting> routingList = routingService.list(new LambdaQueryWrapper<TicketRouting>().eq(TicketRouting::getCode, code));
		routingList.forEach(item->{
			vo.setCode(item.getCode());vo.setName(item.getName());vo.setRemarks(item.getRemarks());vo.setId(item.getId());vo.setIsAutomatically(item.getIsAutomatically());
			TicketRoutingVO.Details bd = new TicketRoutingVO.Details();
			bd.setCode(item.getProcedureCode());bd.setId(item.getProcedureId());
			bd.setName(item.getProcedureName());bd.setInspectionStates(item.getProcedureStates());
			bd.setOrderNumber(item.getOrderNumber());
			details.add(bd);
		});
		vo.setDetails(details);
		return AjaxResult.success(vo);
	}
	
	/**
	 * 是否引用
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/is-quote",method = RequestMethod.GET)
	public AjaxResult isQuote(String code){
		boolean quote = routingService.isQuote(code);
		return AjaxResult.success(quote);
	}
	
}

